home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / binutils / sysdump.c < prev    next >
C/C++ Source or Header  |  1994-04-29  |  12KB  |  745 lines

  1. /* Sysroff object format dumper.
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Binutils.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* Written by Steve Chamberlain <sac@cygnus.com>.
  22.  
  23.    This program reads a SYSROFF object file and prints it in an
  24.    almost human readable form to stdout. */
  25.  
  26. #include <stdio.h>
  27. #include <libiberty.h>
  28. #include <getopt.h>
  29. #include "sysroff.h"
  30. #include <stdlib.h>
  31.  
  32. #define PROGRAM_VERSION "1.0"
  33. static int h8300;
  34. static int sh;
  35. static int dump = 1;
  36. static int segmented_p;
  37. static int code;
  38. static FILE *file;
  39.  
  40. static char *xcalloc(a,b)
  41. int a;
  42. int b;
  43. {
  44.   char *r = xmalloc(a,b);
  45.   memset (r, 0, a * b);
  46.   return r;
  47. }
  48.  
  49. char *
  50. getCHARS (ptr, idx, size, max)
  51.      unsigned char *ptr;
  52.      int *idx;
  53.      int size;
  54.      int max;
  55. {
  56.   int oc = *idx / 8;
  57.   char *r;
  58.   int b = size;
  59.   if (b >= max)
  60.     {
  61.       return "*undefined*";
  62.     }
  63.  
  64.   if (b == 0)
  65.     {
  66.       /* Got to work out the length of the string from self */
  67.       b = ptr[oc++];
  68.       (*idx) += 8;
  69.     }
  70.  
  71.   *idx += b * 8;
  72.   r = calloc (b + 1, 1);
  73.   memcpy (r, ptr + oc, b);
  74.   r[b] = 0;
  75.   return r;
  76. }
  77. static void
  78. dh (ptr, size)
  79.      unsigned char *ptr;
  80.      int size;
  81. {
  82.   int i;
  83.   int j;
  84.   int span = 20;
  85.  
  86.   printf ("\n************************************************************\n");
  87.  
  88.   for (i = 0; i < size; i += span)
  89.     {
  90.       for (j = 0; j < span && j + i < size; j++)
  91.     {
  92.       printf ("%02x ", ptr[i + j]);
  93.     }
  94.       printf ("\n");
  95.     }
  96.  
  97.   for (i = 0; i < size; i += span)
  98.     {
  99.       for (j = 0; j < span && j + i < size; j++)
  100.     {
  101.       int c = ptr[i + j];
  102.       if (c < 32 || c > 127)
  103.         c = '.';
  104.       printf (" %c ", c);
  105.     }
  106.       printf ("\n");
  107.     }
  108. }
  109.  
  110. int 
  111. fillup (ptr)
  112.      char *ptr;
  113. {
  114.   int size;
  115.   int sum;
  116.   int i;
  117.   size = getc (file) - 2;
  118.   fread (ptr, 1, size, file);
  119.   sum = code + size + 2;
  120.   for (i = 0; i < size; i++)
  121.     {
  122.       sum += ptr[i];
  123.     }
  124.  
  125.   if ((sum & 0xff) != 0xff)
  126.     {
  127.       printf ("SUM IS %x\n", sum);
  128.     }
  129.   if (dump)
  130.     dh (ptr, size);
  131.   return size;
  132. }
  133.  
  134.  
  135. barray 
  136. getBARRAY (ptr, idx, dsize, max)
  137.      unsigned char *ptr;
  138.      int *idx;
  139.      int dsize;
  140.      int max;
  141. {
  142.   barray res;
  143.   int i;
  144.   int byte = *idx / 8;
  145.   int size = ptr[byte++];
  146.   res.len = size;
  147.   res.data = (unsigned char *)xmalloc (size);
  148.   for (i = 0; i < size; i++)
  149.     {
  150.       res.data[i] = ptr[byte++];
  151.     }
  152.   return res;
  153. }
  154.  
  155.  
  156.  
  157. int 
  158. getINT (ptr, idx, size, max)
  159.      unsigned char *ptr;
  160.      int *idx;
  161.      int size;
  162.      int max;
  163. {
  164.   int n = 0;
  165.   int byte = *idx / 8;
  166.  
  167.   if (byte >= max)
  168.     {
  169.       return 0;
  170.     }
  171.   if (size == -2)
  172.     size = 4;
  173.   if (size == -1)
  174.     size = 0;
  175.   switch (size)
  176.     {
  177.     case 0:
  178.       return 0;
  179.     case 1:
  180.       n = (ptr[byte]);
  181.       break;
  182.     case 2:
  183.       n = (ptr[byte + 0] << 8) + ptr[byte + 1];
  184.       break;
  185.     case 4:
  186.       n = (ptr[byte + 0] << 24) + (ptr[byte + 1] << 16) + (ptr[byte + 2] << 8) + (ptr[byte + 3]);
  187.       break;
  188.     default:
  189.       abort ();
  190.     }
  191.   *idx += size * 8;
  192.   return n;
  193. }
  194.  
  195. int
  196. getBITS (ptr, idx, size)
  197.      char *ptr;
  198.      int *idx;
  199.      int size;
  200. {
  201.   int byte = *idx / 8;
  202.   int bit = *idx % 8;
  203.  
  204.   *idx += size;
  205.  
  206.   return (ptr[byte] >> (8 - bit - size)) & ((1 << size) - 1);
  207. }
  208.  
  209.  
  210. static void
  211. itheader (name, code)
  212.      char *name;
  213.      int code;
  214. {
  215.   printf ("\n%s 0x%02x\n", name, code);
  216. }
  217. static int indent;
  218. static void
  219. p ()
  220. {
  221.   int i;
  222.   for (i = 0; i < indent; i++)
  223.     {
  224.       printf ("| ");
  225.     }
  226.   printf ("> ");
  227. }
  228.  
  229. static void 
  230. tabout ()
  231. {
  232.   p ();
  233. }
  234.  
  235. static void
  236. pbarray (y)
  237.      barray *y;
  238. {
  239.   int x;
  240.   printf ("%d (", y->len);
  241.   for (x = 0; x < y->len; x++)
  242.     {
  243.       printf ("(%02x %c)", y->data[x], isprint (y->data[x]) ? y->data[x] : '.');
  244.     }
  245.   printf (")\n");
  246. }
  247.  
  248. #define SYSROFF_PRINT
  249. #define SYSROFF_SWAP_IN
  250.  
  251. #include "sysroff.c"
  252.  
  253.  
  254. static int
  255. getone (type)
  256.      int type;
  257. {
  258.   int c = getc (file);
  259.   code = c;
  260.  
  261.   if ((c & 0x7f) != type)
  262.     {
  263.       ungetc (c, file);
  264.       return 0;
  265.     }
  266.  
  267.   switch (c & 0x7f)
  268.     {
  269.     case IT_cs_CODE:
  270.       {
  271.     struct IT_cs dummy;
  272.     sysroff_swap_cs_in (&dummy);
  273.     sysroff_print_cs_out (&dummy);
  274.       }
  275.       break;
  276.     case IT_dln_CODE:
  277.       {
  278.     struct IT_dln dummy;
  279.     sysroff_swap_dln_in (&dummy);
  280.     sysroff_print_dln_out (&dummy);
  281.       }
  282.       break;
  283.     case IT_hd_CODE:
  284.       {
  285.     struct IT_hd dummy;
  286.     sysroff_swap_hd_in (&dummy);
  287.     sysroff_print_hd_out (&dummy);
  288.       }
  289.       break;
  290.     case IT_dar_CODE:
  291.       {
  292.     struct IT_dar dummy;
  293.     sysroff_swap_dar_in (&dummy);
  294.     sysroff_print_dar_out (&dummy);
  295.       }
  296.       break;
  297.     case IT_dsy_CODE:
  298.       {
  299.     struct IT_dsy dummy;
  300.     sysroff_swap_dsy_in (&dummy);
  301.     sysroff_print_dsy_out (&dummy);
  302.       }
  303.       break;
  304.     case IT_dfp_CODE:
  305.       {
  306.     struct IT_dfp dummy;
  307.     sysroff_swap_dfp_in (&dummy);
  308.     sysroff_print_dfp_out (&dummy);
  309.       }
  310.       break;
  311.     case IT_dso_CODE:
  312.       {
  313.     struct IT_dso dummy;
  314.     sysroff_swap_dso_in (&dummy);
  315.     sysroff_print_dso_out (&dummy);
  316.       }
  317.       break;
  318.     case IT_dpt_CODE:
  319.       {
  320.     struct IT_dpt dummy;
  321.     sysroff_swap_dpt_in (&dummy);
  322.     sysroff_print_dpt_out (&dummy);
  323.       }
  324.       break;
  325.     case IT_den_CODE:
  326.       {
  327.     struct IT_den dummy;
  328.     sysroff_swap_den_in (&dummy);
  329.     sysroff_print_den_out (&dummy);
  330.       }
  331.       break;
  332.     case IT_dbt_CODE:
  333.       {
  334.     struct IT_dbt dummy;
  335.     sysroff_swap_dbt_in (&dummy);
  336.     sysroff_print_dbt_out (&dummy);
  337.       }
  338.       break;
  339.     case IT_dty_CODE:
  340.       {
  341.     struct IT_dty dummy;
  342.     sysroff_swap_dty_in (&dummy);
  343.     sysroff_print_dty_out (&dummy);
  344.       }
  345.       break;
  346.     case IT_un_CODE:
  347.       {
  348.     struct IT_un dummy;
  349.     sysroff_swap_un_in (&dummy);
  350.     sysroff_print_un_out (&dummy);
  351.       }
  352.       break;
  353.     case IT_sc_CODE:
  354.       {
  355.     struct IT_sc dummy;
  356.     sysroff_swap_sc_in (&dummy);
  357.     sysroff_print_sc_out (&dummy);
  358.       }
  359.       break;
  360.     case IT_er_CODE:
  361.       {
  362.     struct IT_er dummy;
  363.     sysroff_swap_er_in (&dummy);
  364.     sysroff_print_er_out (&dummy);
  365.       }
  366.       break;
  367.     case IT_ed_CODE:
  368.       {
  369.     struct IT_ed dummy;
  370.     sysroff_swap_ed_in (&dummy);
  371.     sysroff_print_ed_out (&dummy);
  372.       }
  373.       break;
  374.     case IT_sh_CODE:
  375.       {
  376.     struct IT_sh dummy;
  377.     sysroff_swap_sh_in (&dummy);
  378.     sysroff_print_sh_out (&dummy);
  379.       }
  380.       break;
  381.     case IT_ob_CODE:
  382.       {
  383.     struct IT_ob dummy;
  384.     sysroff_swap_ob_in (&dummy);
  385.     sysroff_print_ob_out (&dummy);
  386.       }
  387.       break;
  388.     case IT_rl_CODE:
  389.       {
  390.     struct IT_rl dummy;
  391.     sysroff_swap_rl_in (&dummy);
  392.     sysroff_print_rl_out (&dummy);
  393.       }
  394.       break;
  395.     case IT_du_CODE:
  396.       {
  397.     struct IT_du dummy;
  398.     sysroff_swap_du_in (&dummy);
  399.  
  400.     sysroff_print_du_out (&dummy);
  401.       }
  402.       break;
  403.     case IT_dus_CODE:
  404.       {
  405.     struct IT_dus dummy;
  406.     sysroff_swap_dus_in (&dummy);
  407.     sysroff_print_dus_out (&dummy);
  408.       }
  409.       break;
  410.     case IT_dul_CODE:
  411.       {
  412.     struct IT_dul dummy;
  413.     sysroff_swap_dul_in (&dummy);
  414.     sysroff_print_dul_out (&dummy);
  415.       }
  416.       break;
  417.     case IT_dss_CODE:
  418.       {
  419.     struct IT_dss dummy;
  420.     sysroff_swap_dss_in (&dummy);
  421.     sysroff_print_dss_out (&dummy);
  422.       }
  423.     case IT_hs_CODE:
  424.       {
  425.     struct IT_hs dummy;
  426.     sysroff_swap_hs_in (&dummy);
  427.     sysroff_print_hs_out (&dummy);
  428.       }
  429.  
  430.  
  431.     case IT_dps_CODE:
  432.       {
  433.     struct IT_dps dummy;
  434.     sysroff_swap_dps_in (&dummy);
  435.     sysroff_print_dps_out (&dummy);
  436.       }
  437.       break;
  438.  
  439.  
  440.     case IT_tr_CODE:
  441.       {
  442.     struct IT_tr dummy;
  443.     sysroff_swap_tr_in (&dummy);
  444.     sysroff_print_tr_out (&dummy);
  445.       }
  446.       break;
  447.     case IT_dds_CODE:
  448.       {
  449.     struct IT_dds dummy;
  450.     sysroff_swap_dds_in (&dummy);
  451.     sysroff_print_dds_out (&dummy);
  452.       }
  453.       break;
  454.       break;
  455.     default:
  456.       printf ("GOT A %x\n", c);
  457.       return 0;
  458.       break;
  459.     }
  460.   return 1;
  461.  
  462. }
  463.  
  464. static int
  465. opt (x)
  466.      int x;
  467. {
  468.   return getone (x);
  469. }
  470.  
  471. static void 
  472. unit_info_list ()
  473. {
  474.   if (opt (IT_un_CODE))
  475.     {
  476.       while (getone (IT_sc_CODE))
  477.     {
  478.       getone (IT_ss_CODE);
  479.     }
  480.  
  481.       while (getone (IT_er_CODE))
  482.     {
  483.     }
  484.  
  485.       while (getone (IT_ed_CODE))
  486.     {
  487.     }
  488.     }
  489. }
  490.  
  491. static void
  492. object_body_list ()
  493. {
  494.   getone (IT_sh_CODE);
  495.   while (getone (IT_ob_CODE))
  496.     ;
  497.   while (getone (IT_rl_CODE))
  498.     ;
  499. }
  500.  
  501. static void 
  502. must (x)
  503.      int x;
  504. {
  505.   if (!getone (x))
  506.     {
  507.       printf ("WANTED %x!!\n", x);
  508.     }
  509. }
  510.  
  511. static void 
  512. tab (i, s)
  513.      int i;
  514.      char *s;
  515. {
  516.   indent += i;
  517.   if (s)
  518.     {
  519.       p ();
  520.       printf (s);
  521.       printf ("\n");
  522.     }
  523. }
  524. static void derived_type ();
  525.  
  526. static void
  527. symbol_info ()
  528. {
  529.   tab (1, "SYMBOL INFO");
  530.   while (opt (IT_dsy_CODE))
  531.     {
  532.       if (opt (IT_dty_CODE))
  533.     {
  534.       must (IT_dbt_CODE);
  535.       derived_type ();
  536.       must (IT_dty_CODE);
  537.     }
  538.     }
  539.   tab (-1, "");
  540. }
  541.  
  542. static void
  543. derived_type ()
  544. {
  545.   tab (1, "DERIVED TYPE");
  546.   while (1)
  547.     {
  548.       if (opt (IT_dpp_CODE))
  549.     {
  550.       symbol_info ();
  551.       must (IT_dpp_CODE);
  552.     }
  553.       else if (opt (IT_dfp_CODE))
  554.     {
  555.       symbol_info ();
  556.       must (IT_dfp_CODE);
  557.     }
  558.       else if (opt (IT_den_CODE))
  559.     {
  560.       symbol_info ();
  561.       must (IT_den_CODE);
  562.     }
  563.       else if (opt (IT_den_CODE))
  564.     {
  565.       symbol_info ();
  566.       must (IT_den_CODE);
  567.     }
  568.       else if (opt (IT_dds_CODE))
  569.     {
  570.       symbol_info ();
  571.       must (IT_dds_CODE);
  572.     }
  573.       else if (opt (IT_dar_CODE))
  574.     {
  575.     }
  576.       else if (opt (IT_dpt_CODE))
  577.     {
  578.     }
  579.       else if (opt (IT_dul_CODE))
  580.     {
  581.     }
  582.       else if (opt (IT_dse_CODE))
  583.     {
  584.     }
  585.       else if (opt (IT_dot_CODE))
  586.     {
  587.     }
  588.       else
  589.     break;
  590.     }
  591.  
  592.   tab (-1, "");
  593. }
  594.  
  595. static void
  596. program_structure ()
  597. {
  598.   tab (1, "PROGRAM STRUCTURE");
  599.   while (opt (IT_dps_CODE))
  600.     {
  601.       opt (IT_dso_CODE);
  602.       opt (IT_dss_CODE);
  603.       symbol_info ();
  604.     }
  605.   tab (-1, "");
  606. }
  607. static void
  608. debug_list ()
  609. {
  610.   tab (1, "DEBUG LIST");
  611.   getone (IT_du_CODE);
  612.   while (getone (IT_dus_CODE))
  613.     ;
  614.   while (opt (IT_dfl_CODE))
  615.     ;
  616.   while (getone (IT_dus_CODE))
  617.     ;
  618.  
  619.   program_structure ();
  620.  
  621.   getone (IT_dln_CODE);
  622.   tab (-1, "");
  623. }
  624.  
  625. static void
  626. module ()
  627. {
  628.   int c = 0;
  629.   int l = 0;
  630.   
  631.   tab (1, "MODULE***\n");
  632.  
  633.   getone (IT_cs_CODE);
  634.   getone (IT_hd_CODE);
  635.   getone (IT_hs_CODE);
  636.  
  637.   while (!opt (IT_tr_CODE) && c < 10)
  638.     {
  639.       unit_info_list ();
  640.       object_body_list ();
  641.       debug_list ();
  642. c++;
  643.     }
  644.   tab (-1, "");
  645.  
  646.   c = getc (file);
  647.   while (c != EOF)
  648.     {
  649.       printf ("%02x ", c);
  650.       l++;
  651.       if (l == 32)
  652.     {
  653.       printf ("\n");
  654.       l = 0;
  655.     }
  656.       c = getc (file);
  657.     }
  658. }
  659.  
  660.  
  661. char * program_name;
  662.  
  663. static void
  664. show_usage (file, status)
  665.      FILE *file;
  666.      int status;
  667. {
  668.   fprintf (file, "Usage: %s [-hV] in-file\n",   program_name);
  669.   exit (status);
  670. }
  671.  
  672. static void
  673. show_help ()
  674. {
  675.   printf ("%s: Print a human readable interpretation of a SYSROFF object file\n",
  676.       program_name);
  677.   show_usage (stdout, 0);
  678. }
  679.  
  680.  
  681. int 
  682. main (ac, av)
  683.      int ac;
  684.      char **av;
  685. {
  686.   char *input_file = NULL;
  687.   int opt;
  688.   static struct option long_options[] =
  689.     {
  690.       { "help", no_argument, 0, 'h' },
  691.       { "version", no_argument, 0, 'V' },
  692.       { NULL, no_argument, 0, 0 }
  693.     };
  694.  
  695.   program_name = av[0];
  696.   xmalloc_set_program_name (program_name);
  697.  
  698.   while ((opt = getopt_long (ac, av, "hV", long_options,
  699.                  (int *) NULL))
  700.      != EOF)
  701.     {
  702.       switch (opt)
  703.     {
  704.     case 'h':
  705.       show_help ();
  706.       /*NOTREACHED*/
  707.     case 'V':
  708.       printf ("GNU %s version %s\n", program_name, PROGRAM_VERSION);
  709.       exit (0);
  710.       /*NOTREACHED*/
  711.     case 0:
  712.       break;
  713.     default:
  714.       show_usage (stderr, 1);
  715.       /*NOTREACHED*/
  716.     }
  717.     }
  718.  
  719.   /* The input and output files may be named on the command line.  */
  720.  
  721.   if (optind < ac)
  722.     {
  723.       input_file = av[optind];
  724.     }
  725.  
  726.   if (!input_file)
  727.     {
  728.       fprintf (stderr,"%s: no input file specified\n",
  729.            program_name);
  730.       exit(1);
  731.     }
  732.  
  733.   file = fopen (input_file, "r");
  734.   if (!file)
  735.     {
  736.       fprintf (stderr,"%s: cannot open input file %s\n",
  737.            program_name, input_file);
  738.       exit(1);
  739.     }
  740.  
  741.  
  742.   module ();
  743.   return 0;
  744. }
  745.